package com.doone.telmarket.admin.action;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;
import java.math.BigDecimal;
import java.util.List;

import javax.imageio.ImageIO;
import javax.servlet.ServletContext;
import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;
import javax.servlet.http.HttpSession;

import org.apache.log4j.Logger;
import org.apache.struts2.ServletActionContext;
import org.apache.struts2.StrutsStatics;
import org.apache.struts2.convention.annotation.Action;
import org.apache.struts2.convention.annotation.InterceptorRef;
import org.apache.struts2.convention.annotation.Result;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import com.doone.telmarket.domain.Currency;
import com.doone.telmarket.domain.ServicePage;
import com.doone.telmarket.domain.ServicePageBean;
import com.doone.telmarket.domain.service.FileUploadService;
import com.doone.telmarket.domain.service.ServiceOpration;
import com.doone.telmarket.domain.service.SitePageGenerative;
import com.googlecode.jsonplugin.JSONUtil;
import com.opensymphony.xwork2.ActionContext;
import com.opensymphony.xwork2.ActionSupport;

/**
 * @package com.doone.telmarket.admin.action
 * @author 甘雨波
 * @date 2009-09-15 15:43
 * @description 增值业务配置ACTION
 *
 */
@Service("serviceAction")
public class ServiceAction extends ActionSupport {

	/**
	 * 
	 */
	private static final long serialVersionUID = 1L;

	Logger log = Logger.getLogger(ServiceTypeAction.class);

	/*缩略图高度下限*/
	public static final int SMALL_IMAGE_HEIGHT_LT = 55;
	/*缩略图高度上限*/
	public static final int SMALL_IMAGE_HEIGHT_GT = 75;
	/*缩略图宽度下限*/
	public static final int SMALL_IMAGE_WIDTH_LT = 80;
	/*缩略图宽度上限*/
	public static final int SMALL_IMAGE_WIDTH_GT = 100;
	/*LOGO图高度下限*/
	public static final int BIG_IMAGE_HEIGHT_LT = 167;
	/*LOGO图高度上限*/
	public static final int BIG_IMAGE_HEIGHT_GT = 187;
	/*LOGO图宽度下限*/
	public static final int BIG_IMAGE_WIDTH_LT = 188;
	/*LOGO图宽度上限*/
	public static final int BIG_IMAGE_WIDTH_GT = 208;

	/****/
	private File[] file;

	private String[] fileContentType;

	private String[] fileFileName;

	private String page;

	private String serviceId;

	private String serviceName;

	private String serviceType;

	private String serviceClassify;

	private String value;

	private String serviceDesc;

	private String serviceCodeGrade;

	private String errorMessage;

	private String spName;

	private String serviceCodeType;

	private String serviceContent;

	private ServicePage servicePage;

	private String saveType;

	private List<com.doone.telmarket.domain.Service> serviceList;

	@Autowired
	private ServiceOpration serviceOprationAble;
	
	@Autowired
	private FileUploadService fileOprationAble;
	
	@Autowired
	private SitePageGenerative sitePageGenerative;

	/**
	 * @package org.springframework.stereotype.Service
	 * @author 甘雨波
	 * @description 新增增值业务
	 * @date 2009-09-16 12:05
	 * @return
	 */
	@Action(value = "saveService", interceptorRefs = { @InterceptorRef("fileUploadStack") }, results = {
			@Result(name = "success", location = "/admin/success.jsp"),
			@Result(name = "error", location = "/admin/error.jsp") })
	public String saveService() {

		BufferedImage bi;
		try {
			bi = ImageIO.read(file[0]);
			int height = bi.getHeight();
			int width = bi.getWidth();
			if (SMALL_IMAGE_HEIGHT_LT > height || SMALL_IMAGE_HEIGHT_GT < height || SMALL_IMAGE_WIDTH_LT > width
					|| SMALL_IMAGE_WIDTH_GT < width) {
				errorMessage = "增值业务缩略图大小不合法";
				return "error";
			}
			bi = ImageIO.read(file[1]);
			height = bi.getHeight();
			width = bi.getWidth();
			if (BIG_IMAGE_HEIGHT_LT > height || BIG_IMAGE_HEIGHT_GT < height || BIG_IMAGE_WIDTH_LT > width
					|| BIG_IMAGE_WIDTH_GT < width) {
				errorMessage = "增值业务LOGO图大小不合法";
				return "error";
			}

		} catch (IOException e) {
			e.printStackTrace();
			errorMessage = "验证图片尺寸大小失败";
			return "error";
		}

		servicePage = serviceOprationAble.oprationData(serviceId, serviceCodeType, serviceName, serviceDesc, value,
				spName, "E", serviceType, serviceClassify, serviceCodeGrade, serviceContent,fileFileName);

		if (servicePage != null) {
			
			fileFileName[0] = servicePage.getSmallIconPath();
			fileFileName[1] = servicePage.getLogoPath();
			fileFileName[2] = servicePage.getFlashPath();
			fileOprationAble.uploadFiles(file, fileFileName);
			ActionContext.getContext().getSession().put("servicePage", servicePage);
			page = "service.jsp";
			return "success";
			
		} else {
			errorMessage = "处理页面数据失败";
			return "error";
		}
	}

	/**
	 * @package org.springframework.stereotype.Service
	 * @author 甘雨波
	 * @description 保存新增增值业务的操作数据及生成静态页面
	 * @date 2009-09-18 14:49
	 * @return
	 */
	@Action(value = "saveOpration")
	public void saveOpration() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=utf-8");
		try {
			servicePage = (ServicePage) ActionContext.getContext().getSession().get("servicePage");

			HttpServletRequest request = (HttpServletRequest) ActionContext.getContext().get(
					org.apache.struts2.StrutsStatics.HTTP_REQUEST);
			HttpSession session = request.getSession();

			ServletContext sc = (ServletContext) ActionContext.getContext().get(StrutsStatics.SERVLET_CONTEXT);
			String path = sc.getRealPath("/");
			//保存增值业务
			if ("update".equals(saveType)) {
				serviceOprationAble.updateService(servicePage);
			} else {
				serviceOprationAble.saveService(servicePage);
			}
			//生成页面
			servicePage.setPageUrl(servicePage.getPagePath());
			String pagePath = "http://127.0.0.1:8080/vasm-web/admin/serviceTemplet.jsp;jsessionid=" + session.getId();
			sitePageGenerative.generatePage(path, pagePath, servicePage.getPage());
			
			response.getWriter().write("生成页面成功");
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("生成页面失败");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}

	/**
	 * 
	 * @author 甘雨波
	 * @description  根据类型取得SERVICE
	 * @date 2009-9-21
	 */
	@SuppressWarnings("static-access")
	@Action(value = "getServiceByType")
	public void getServiceByType() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=utf-8");
		try {
			serviceList = serviceOprationAble.selectServiceByType(serviceType, serviceClassify);
			JSONUtil ju = new JSONUtil();
			String jsonString = ju.serialize(serviceList);
			//log.info(ju.serialize(serviceList));
			response.getWriter().write(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @author 甘雨波
	 * @description  根据SERVICEID取得SERVICE
	 * @date 2009-9-21
	 */
	@SuppressWarnings("static-access")
	@Action(value = "selectServiceById")
	public void getServiceById() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=utf-8");
		try {
			com.doone.telmarket.domain.Service service = serviceOprationAble.selectServiceById(serviceId);
			JSONUtil ju = new JSONUtil();
			Currency c = new Currency();
			c.setMoney(service.getServiceCost().getMoney().divide(new BigDecimal(100)));
			service.setServiceCost(c);
			String jsonString = ju.serialize(service);
			log.info(ju.serialize(service));
			response.getWriter().write(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	/**
	 * 
	 * @author 甘雨波
	 * @description  根据SERVICEID取得SERVICEPAGE
	 * @date 2009-9-21
	 */
	@SuppressWarnings("static-access")
	@Action(value = "selectServicePageByServiceId")
	public void selectServicePageByServiceId() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=utf-8");
		try {
			JSONUtil ju = new JSONUtil();
			ServicePage sp = serviceOprationAble.selectServicePageByServiceId(serviceId);
			ServicePageBean spb = new ServicePageBean();
			spb.setServiceId(serviceId);
			spb.setPageFlashPath(sp.getPageFlashPath());
			spb.setPageLogoPath(sp.getPageLogoPath());
			spb.setPageSmallIconPath(sp.getPageSmallIconPath());
			spb.setPageUrl(sp.getPageUrl());
			spb.setRecommandGrade(sp.getRecommandGrade());
			spb.setServiceContent(sp.getServiceContent());
			String jsonString = ju.serialize(spb);
			response.getWriter().write(jsonString);
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}
	}

	@Action(value = "deleteById")
	public void deleteService() {
		HttpServletResponse response = ServletActionContext.getResponse();
		response.setContentType("text/json;charset=utf-8");
		try {
			int count = serviceOprationAble.deleteServiceById(serviceId);
			if (count != 0) {
				response.getWriter().write("E");
			} else {
				response.getWriter().write("F");
			}
		} catch (Exception e) {
			e.printStackTrace();
			try {
				response.getWriter().write("F");
			} catch (IOException e1) {
				e1.printStackTrace();
			}
		}

	}

	public String[] getFileContentType() {
		return fileContentType;
	}

	public void setFileContentType(String[] fileContentType) {
		this.fileContentType = fileContentType;
	}

	public String[] getFileFileName() {
		return fileFileName;
	}

	public void setFileFileName(String[] fileFileName) {
		this.fileFileName = fileFileName;
	}

	public File[] getFile() {
		return file;
	}

	public void setFile(File[] file) {
		this.file = file;
	}

	public String getPage() {
		return page;
	}

	public void setPage(String page) {
		this.page = page;
	}

	public String getServiceId() {
		return serviceId;
	}

	public void setServiceId(String serviceId) {
		this.serviceId = serviceId;
	}

	public String getServiceName() {
		return serviceName;
	}

	public void setServiceName(String serviceName) {
		this.serviceName = serviceName;
	}

	public String getServiceType() {
		return serviceType;
	}

	public void setServiceType(String serviceType) {
		this.serviceType = serviceType;
	}

	public String getServiceClassify() {
		return serviceClassify;
	}

	public void setServiceClassify(String serviceClassify) {
		this.serviceClassify = serviceClassify;
	}

	public String getValue() {
		return value;
	}

	public void setValue(String value) {
		this.value = value;
	}

	public String getServiceDesc() {
		return serviceDesc;
	}

	public void setServiceDesc(String serviceDesc) {
		this.serviceDesc = serviceDesc;
	}

	public String getServiceCodeGrade() {
		return serviceCodeGrade;
	}

	public void setServiceCodeGrade(String serviceCodeGrade) {
		this.serviceCodeGrade = serviceCodeGrade;
	}

	public String getErrorMessage() {
		return errorMessage;
	}

	public String getSpName() {
		return spName;
	}

	public void setSpName(String spName) {
		this.spName = spName;
	}

	public String getServiceCodeType() {
		return serviceCodeType;
	}

	public void setServiceCodeType(String serviceCodeType) {
		this.serviceCodeType = serviceCodeType;
	}

	public String getServiceContent() {
		return serviceContent;
	}

	public void setServiceContent(String serviceContent) {
		this.serviceContent = serviceContent;
	}

	public ServicePage getServicePage() {
		return servicePage;
	}

	public void setServicePage(ServicePage servicePage) {
		this.servicePage = servicePage;
	}

	public void setErrorMessage(String errorMessage) {
		this.errorMessage = errorMessage;
	}

	public List<com.doone.telmarket.domain.Service> getServiceList() {
		return serviceList;
	}

	public void setServiceList(List<com.doone.telmarket.domain.Service> serviceList) {
		this.serviceList = serviceList;
	}

	public String getSaveType() {
		return saveType;
	}

	public void setSaveType(String saveType) {
		this.saveType = saveType;
	}

}
